import handler.DownLoadHandler;
import handler.DownLoadHandler2;
import handler.DownLoadHandler3;
import handler.ListFileHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;

import java.io.File;

public class HttpServer
{
    public void start() throws InterruptedException
    {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup());
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception
            {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new HttpRequestDecoder());
                pipeline.addLast(new HttpResponseEncoder());
                pipeline.addLast(new HttpObjectAggregator(1024));
                pipeline.addLast(new ListFileHandler(new File("z:/")));
                pipeline.addLast(new DownLoadHandler3(new File("z:/")));
            }
        });
        ChannelFuture bind = bootstrap.bind(80);
        bind.sync();
        bind.channel().closeFuture().sync();
    }

    public static void main(String[] args) throws InterruptedException
    {
        new HttpServer().start();
    }
}
